import socket
from application import app

class WebServer(object):
    def __init__(self):
        tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        tcp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)        
        tcp_socket.bind(('', 8080))
        tcp_socket.listen(128)
        self.tcp_socket = tcp_socket
    def start(self):
         while True:
            new_socket, ip_port = self.tcp_socket.accept()
            print('%s上线啦' %str(ip_port))
            self.request_heandler(new_socket, ip_port)

    def request_heandler(self, new_socket, ip_port):
        app.application(new_socket, ip_port)
        new_socket.close()

def main():
    ws = WebServer()
    ws.start()

if __name__ == '__main__':
    main()
    